package com.mida.app.report.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.mida.app.report.service.DetailCodingSystemService;
import com.mida.app.report.service.MdmService;
import com.mida.app.report.vo.HospitolAndExamTypeVo;
import com.mida.dms.mdm.api.provider.*;
import com.mida.dms.mdm.dto.ParameterDto;
import com.mida.dms.mdm.dto.UserDto;
import com.mida.dms.mdm.pojo.auto.MsdDataSet;
import com.mida.dms.mdm.pojo.auto.MsdResourceInfo;
import com.mida.dms.mdm.vo.provider.BaseOrgInfoVo;
import com.mida.dms.mdm.vo.provider.BusinessUnitVo;
import com.mida.dms.mdm.vo.provider.EmployeeInfoVo;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.mida.tool.common.result.BaseResult.buildListSuccess;
import static com.mida.tool.common.result.BaseResult.buildListWarn;

/**
 * @ClassName: MdmServiceImpl
 * @Description: TODO
 * @Author: wang xiao le
 * @Date: 2021/6/19 10:04
 **/
@Service
@Slf4j
@Transactional
public class MdmServiceImpl implements MdmService {

    /**
     * 员工相关查询feign接口
     */
    @Autowired
    private IEmployeeInfoApi iEmployeeInfoApi;

    /**
     * 业务单元相关查询
     */
    @Autowired
    private IBusinessUnitApi iBusinessUnitApi;
    /**
     * 获取当前用户信息
     */
    @Autowired
    private IAuthApi iAuthApi;
    /**
     * 集团、医院、科室相关查询feign接口
     */
    @Autowired
    private IOrgInfoApi iOrgInfoApi;

    @Autowired
    private IParameterApi iParameterApi;

    @Autowired
    private IDataSetApi iDataSetApi;
    @Autowired
    private IResourceInfoApi iResourceInfoApi;

    @Autowired
    private DetailCodingSystemService detailCodingSystemService;
    /**
     * 1、根据当前用户员工soid查询医院
     *
     * @return
     */
    @Override
    public ListResult<List<BaseOrgInfoVo>> findHospitalByEmployeeSoid(Long employeeSoid1) {
        return iOrgInfoApi.findHospitalByEmployeeSoid(employeeSoid1);
    }

    /**
     * 2、查询医院下的所有业务单元
     *
     * @param hospitalSoid 医院soid
     * @param buTypeCode   业务单元类型
     * @param search       检索条件:名称，代码，拼音码，五笔码
     */
    @Override
    public ListResult<List<BusinessUnitVo>> findBusinessUnitListUnderHospital(Long hospitalSoid, Long buTypeCode, String search) {
        return iBusinessUnitApi.findBusinessUnitListUnderHospital(hospitalSoid, buTypeCode, search);
    }

    /**
     * 3.1根据医院soid模糊查询当前医院下员工信息
     *
     * @param search      检索条件:工号,名称,拼音码，五笔码，
     * @param orgInfoSoid 组织soid （医院soid）
     * @param orgInfoType 组织类型:集团-2585、医院-2586、科室-2587
     * @return
     */
    @Override
    public ListResult<List<EmployeeInfoVo>> findListByOrgSoid(Long orgInfoSoid, long orgInfoType, String search) {
        return iEmployeeInfoApi.findListByOrgSoid(orgInfoSoid, orgInfoType, search);
    }

    /**
     * 3.2 根据业务单元soid模糊查询员工信息
     *
     * @param search              检索条件:工号,名称,拼音码，五笔码，
     * @param businessUnitSoidStr 业务单元soid集合
     * @return
     */
    @Override
    public ListResult<List<EmployeeInfoVo>> findListByBusinessUnitSoidList(String businessUnitSoidStr, String search) {
        return iEmployeeInfoApi.findListByBusinessUnitSoidList(businessUnitSoidStr, search);
    }

    /**
     * 根据组织soid查询组织
     *
     * @param orgInfoSoid     组织soid
     * @param isIncludeDelete 是否包含已删除的数据(回显的时候需要用到)
     * @return
     */
    @Override
    public ListResult<BaseOrgInfoVo> findOrgByOrgInfoSoid(Long orgInfoSoid, boolean isIncludeDelete) {
        return iOrgInfoApi.findOrgByOrgInfoSoid(orgInfoSoid, false);
    }

    /**
     * 根据soid查询业务单元
     *
     * @param businessUnitSoid 业务单元soid
     * @param isIncludeDelete  是否包含已删除的数据(回显的时候需要用到),true-包含，false-不包含
     */
    @Override
    public ListResult<BusinessUnitVo> findBusinessUnitBySoid(Long businessUnitSoid, boolean isIncludeDelete) {
        return iBusinessUnitApi.findBusinessUnitBySoid(businessUnitSoid, false);
    }

    /**
     * 根据员工soid查询
     *
     * @param employeeSoid    员工soid
     * @param isIncludeDelete 是否包含已删除、已停用的记录
     */
    @Override
    public ListResult<EmployeeInfoVo> findOneBySoid(Long employeeSoid, boolean isIncludeDelete) {
        return iEmployeeInfoApi.findOneBySoid(employeeSoid, false);
    }

    /**
     * 查询员工所属的所有业务单元
     *
     * @param employeeSoid 员工soid
     * @param buTypeCode   业务单元类型soid 非必填 2951-临床业务单元，2954-护理业务单元，3572-医技业务单元
     * @return
     */
    @Override
    public ListResult<List<BusinessUnitVo>> findBusinessUnitListByEmployeeSoid(Long employeeSoid, Long buTypeCode) {
        return iBusinessUnitApi.findBusinessUnitListByEmployeeSoid(employeeSoid, null);
    }
    /**
     * 查询当前登录用户信息
     * @return UserDto
     */
    @Override
    public ListResult<UserDto> getLoginUserInfo() {

        ListResult<UserDto> loginUserInfo = iAuthApi.getLoginUserInfo();
        return loginUserInfo;
    }

    @Override
    public ListResult<ParameterDto> findParameterByOrgInfoSoid(Long var1, Long var2, Long var3) {
        ListResult<com.mida.dms.mdm.dto.ParameterDto> parameterByOrgInfoSoid = iParameterApi.findParameterByOrgInfoSoid(var1, var2, var3);
        ListResult<ParameterDto> parameterDtoListResult = new ListResult<>();
        BeanUtils.copyProperties(parameterByOrgInfoSoid,parameterDtoListResult);
        return parameterDtoListResult;
    }

    @Override
    public String getDataSetName(BigDecimal soid) {
        String name = "";
        if (com.baomidou.mybatisplus.core.toolkit.ObjectUtils.isNotEmpty(soid)) {
            com.mida.tool.common.result.ListResult<MsdDataSet> conceptSoidByDataSetSoid =
                    iDataSetApi.getConceptSoidByDataSetSoid(soid.longValueExact());
            if (ObjectUtil.isNotEmpty(conceptSoidByDataSetSoid.getData()) && ObjectUtil.isNotEmpty(conceptSoidByDataSetSoid.getData().getDataSetMeaning())) {
                name = conceptSoidByDataSetSoid.getData().getDataSetMeaning();
            }
        }
        return name;
    }

    /**
     * 批量查询员工信息 key--employeeSoid ,value--employeeName
     * @param employeeSoids 员工soid list 必填
     * @return
     */
    @Override
    public Map<Long, String> findListBySoids(List<Long> employeeSoids) {

        ListResult<List<EmployeeInfoVo>> listBySoids = iEmployeeInfoApi.findListBySoids(employeeSoids);
        Map<Long, String> EmployeeMap = new HashMap<>();
        if (ObjectUtils.isNotEmpty(listBySoids.getData())) {
            List<EmployeeInfoVo> dataSetList = listBySoids.getData();
            //将值集soid为key,值集name为value放入map中
            for (EmployeeInfoVo employeeInfoVo : dataSetList) {
                if (EmployeeMap.get(Long.valueOf(employeeInfoVo.getEmployeeSoid())) == null) {
                    EmployeeMap.put(Long.valueOf(employeeInfoVo.getEmployeeSoid()), employeeInfoVo.getEmployeeName());
                }
            }

        }
        return EmployeeMap;
    }

    @Override
    public ListResult<HospitolAndExamTypeVo> queryHospitolAndExamType() {
        HospitolAndExamTypeVo hospitolAndExamTypeVo = new HospitolAndExamTypeVo();
        ListResult<HospitolAndExamTypeVo> lr = new ListResult<>();
        ListResult<UserDto> loginUserInfo = iAuthApi.getLoginUserInfo();
        UserDto user = loginUserInfo.getData();
        Long employeeSoid = user.getEmployeeSoid();
        if (ObjectUtil.isEmpty(loginUserInfo) && ObjectUtil.isEmpty(employeeSoid)) {
            buildListWarn(lr, "用户信息为空");
            return lr;
        }
        Long curBusinessUnitSoid = user.getCurBusinessUnitSoid();
        BusinessUnitVo businessUnitVo = iBusinessUnitApi
                .findBusinessUnitBySoid(curBusinessUnitSoid, false)
                .getData();
        if (ObjectUtil.isNotEmpty(businessUnitVo)) {
            //保存学科的soid
            String Type = businessUnitVo.getSubjectRelationSoidList().get(0);
            hospitolAndExamTypeVo.setExamServiceType(Type);
            String dataSetName = detailCodingSystemService.getDataSetName(Long.valueOf(Type));
            //保存学科类型名称
            hospitolAndExamTypeVo.setExamServiceTypeName(dataSetName);
        }
        hospitolAndExamTypeVo.setOrgInfoSoid(String.valueOf(user.getHospitalSoid()));
        hospitolAndExamTypeVo.setOrgInfoName(user.getHospitalName());

        buildListSuccess(lr, hospitolAndExamTypeVo);
        return lr;
    }
    @Override
    public ListResult<List<MsdResourceInfo>> findListByMediaImageType(List<Long> imageTypeList) {

        return iResourceInfoApi.findListByMediaImageType(imageTypeList);
    }
}
